package cc.rengu.igas.opms.core.service.trans.common;

import cc.rengu.igas.opms.common.constant.OpmsParamConstant;
import cc.rengu.igas.opms.common.dao.*;
import cc.rengu.igas.opms.common.dao.impl.*;
import cc.rengu.igas.opms.common.entity.*;
import cc.rengu.igas.opms.common.enums.RespCodeEnum;
import cc.rengu.igas.opms.core.model.BankGatewayBean;
import cc.rengu.igas.opms.core.service.base.ManageService;
import cc.rengu.igas.opms.facade.bean.SupportBankInfo;
import cc.rengu.igas.opms.facade.request.SurpBankListQueryRequest;
import cc.rengu.igas.opms.facade.response.SurpBankListQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * 支持银行列表查询服务
 *
 * @author keyi
 * @modify by wangcheng
 * @date 2020/4/5.
 */
public class SurpBankListQueryService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        SurpBankListQueryRequest surpBankListQueryRequest = new SurpBankListQueryRequest();
        ConvertUtil.convertOutput(surpBankListQueryRequest);
        return surpBankListQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {

        SurpBankListQueryRequest surpBankListQueryRequest = (SurpBankListQueryRequest) request;
        String instId = surpBankListQueryRequest.getHeader().getInstId();
        String queryType = surpBankListQueryRequest.getQueryType();

        //产品类型转换 并查询得到响应数据
        ArrayList<SupportBankInfo> supportBankInfos = new ArrayList<>();
        switch (queryType) {
            case "00":
                //个人网银支付
                getSupportInstData(supportBankInfos, instId, surpBankListQueryRequest.getHeader().getChanlId(), "10");
                break;
            case "01":
                //企业网银支付
                getSupportInstData(supportBankInfos, instId, surpBankListQueryRequest.getHeader().getChanlId(), "11");
                break;
            case "02":
                //银行卡快捷支付
                getSupportInstData(supportBankInfos, instId, surpBankListQueryRequest.getHeader().getChanlId(), "20");
                break;
            case "03":
                //个人/企业网银支付
                getSupportInstData(supportBankInfos, instId, surpBankListQueryRequest.getHeader().getChanlId(), "10");
                getSupportInstData(supportBankInfos, instId, surpBankListQueryRequest.getHeader().getChanlId(), "11");
                break;
            default:
                rglog.info("无法识别传入查询类型：prodType:<{}>", queryType);
                throw new BizException(RespCodeEnum.PARAM_ERROR.getRespCode(), RespCodeEnum.PARAM_ERROR.getRespDesc());
        }

        //组装响应信息
        SurpBankListQueryResponse surpBankListQueryResponse = new SurpBankListQueryResponse();
        surpBankListQueryResponse.setHeader(surpBankListQueryRequest.getHeader());
        surpBankListQueryResponse.setSurportList(supportBankInfos);
        BizResponse<SurpBankListQueryResponse> bizResponse = new BizResponse<>();
        bizResponse.setResult(surpBankListQueryResponse);
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        SurpBankListQueryResponse surpBankListQueryResponse = (SurpBankListQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(surpBankListQueryResponse);
    }

    /**
     * 计算借贷记支持标识
     *
     * @param debitSupportFlag  借记支持标识
     * @param creditSupportFlag 贷记支持标识
     * @return 支持信息 0:借贷记 1:借记 2:贷记 3:都不支持
     */
    private String debitCreditFlag(String debitSupportFlag, String creditSupportFlag) {

        String debitCreditFlag;
        if (AppParamConstant.YES.equals(creditSupportFlag) && AppParamConstant.YES.equals(debitSupportFlag)) {
            debitCreditFlag = OpmsParamConstant.STR_ZERO;
        } else if (!AppParamConstant.YES.equals(creditSupportFlag) && AppParamConstant.YES.equals(debitSupportFlag)) {
            debitCreditFlag = OpmsParamConstant.STR_ONE;
        } else if (AppParamConstant.YES.equals(creditSupportFlag)) {
            debitCreditFlag = OpmsParamConstant.STR_TWO;
        } else {
            debitCreditFlag = OpmsParamConstant.STR_THREE;
        }
        return debitCreditFlag;
    }

    /**
     * 查询银行支持列表并转换为响应信息
     *
     * @param supportBankInfos 响应信息集合
     * @param instId           机构号
     * @param bizChanlType     请求渠道
     * @param supportBizType   支持业务类别
     * @throws Exception 异常
     */
    private void getSupportInstData(ArrayList<SupportBankInfo> supportBankInfos, String instId, String bizChanlType, String supportBizType) throws Exception {
        //查询银行支持列表
        BankSupportCfgMapper opmsBankSupportCfgMapper = new BankSupportCfgMapperImpl();
        List<BankSupportCfgInfo> opmsBankSupportCfgInfos = opmsBankSupportCfgMapper.selectOpmsBankSupportCfgByInstIdSupportBizType(instId, supportBizType);

        //组应答体
        SupportBankInfo supportBankInfo;
        for (BankSupportCfgInfo opmsBankSupportCfgInfo : opmsBankSupportCfgInfos) {

            if (AppParamConstant.NO.equals(opmsBankSupportCfgInfo.getRecordStatus())) {
                rglog.debug("当前数据无效，bankSupportCfg:<{}>", JSON.toJSONString(opmsBankSupportCfgInfo));
                continue;
            }
            supportBankInfo = new SupportBankInfo();
            if (!StringUtil.isEmptyOrNull(opmsBankSupportCfgInfo.getOtherData())) {
                /* 银联网关支付 */
                if (("10".equals(supportBizType) || "11".equals(supportBizType))) {
                    List<BankGatewayBean> bankGatewayBeanList = JSON.parseArray(opmsBankSupportCfgInfo.getOtherData(), BankGatewayBean.class);
                    Optional<BankGatewayBean> bankGatewayBeanOpt = bankGatewayBeanList.stream().filter(item -> item.getGatewayChanlType().equals(bizChanlType)).findFirst();
                    if (bankGatewayBeanOpt.isPresent()) {
                        supportBankInfo.setBankLogoUrl(bankGatewayBeanOpt.get().getBankLogoPath());
                        supportBankInfo.setGatewayChanlId(bankGatewayBeanOpt.get().getGatewayChanlId());
                        supportBankInfo.setGatewayType("10".equals(supportBizType) ? "00" : "01");
                    } else {
                        rglog.warn("银联网关配置{}中没有配置业务渠道为<{}>的信息!", opmsBankSupportCfgInfo.getOtherData(), bizChanlType);
                        continue;
                    }
                }
            } else {
                supportBankInfo.setBankLogoUrl(opmsBankSupportCfgInfo.getBankLogoUrl());
            }
            supportBankInfo.setBankNo(opmsBankSupportCfgInfo.getBankNo());
            supportBankInfo.setDebitCreditFlag(debitCreditFlag(opmsBankSupportCfgInfo.getDebitSupportFlag(), opmsBankSupportCfgInfo.getCreditSupportFlag()));
            supportBankInfo.setBankSimpleCode(opmsBankSupportCfgInfo.getBankSimpleCode());
            supportBankInfo.setBankName(opmsBankSupportCfgInfo.getBankName());
            supportBankInfo.setSingleLimitAmt(opmsBankSupportCfgInfo.getSingleLimitAmt());
            supportBankInfo.setDayLimitAmt(opmsBankSupportCfgInfo.getDayLimitAmt());
            supportBankInfo.setDiscountFlag(opmsBankSupportCfgInfo.getMcFlag());
            supportBankInfo.setDiscountDesc(opmsBankSupportCfgInfo.getMcName());
            supportBankInfos.add(supportBankInfo);
        }
    }
}
